package portal.controller;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import beans.BaseResourceState;
import portal.dao.ScheduleClusterHostDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ClusterResourceUsage extends BaseServlet {

    private static final long serialVersionUID = 3239412217633667238L;

    @Autowired
    public ScheduleClusterHostDao scheduleClusterHostDao ;
    public ClusterResourceUsage() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String operateType = request.getParameter("operateType");

        switch (operateType) {
            case "showHosts":
                showHosts(request, response);
                break;
            case "searchHostByHostName":
                searchHostByHostName(request, response);
                break;
            case "getHostHistoryState":
                getHostHistoryState(request, response);
                break;
            default:
                break;
        }
    }

    /**
     * By黄振 本地数据库中，查询所有计算节点最新详情
     * @param request
     * @param response
     * @throws IOException
     */
    private void showHosts(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("userId");
        Integer onePageCount = Integer.parseInt(request.getParameter("onePageCount"));
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        //ScheduleClusterHostDao scheduleClusterHostDao = new ScheduleClusterHostDao();
        List<Object> resourceList = scheduleClusterHostDao.getClusterHosts(userId, onePageCount, currentPage);
        Long pageNum = scheduleClusterHostDao.getHostsPageNum(onePageCount);
        JSONObject result = new JSONObject();
        JSONObject content = new JSONObject();
        content.put("hosts", JSONArray.fromObject(resourceList));
        content.put("pageNum", pageNum);
        result.put("result", content);
        response.getWriter().print(result.toString());
    }

    /**
     * By黄振  本地数据库中，根据节点名称查询节点
     * @param request
     * @param response
     * @throws IOException
     */
    private void searchHostByHostName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        String userId = (String) session.getAttribute("userId");
        String hostName = request.getParameter("hostName");
        Integer onePageCount = Integer.parseInt(request.getParameter("onePageCount"));
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        //ScheduleClusterHostDao scheduleClusterHostDao = new ScheduleClusterHostDao();
        List<Object> resourceList = scheduleClusterHostDao.searchHostByHostName(hostName,onePageCount,currentPage);
        long pageNum = scheduleClusterHostDao.getHostsPageNumByHostName(onePageCount,hostName);
        JSONObject result = new JSONObject();
        JSONObject content = new JSONObject();
        content.put("hosts", JSONArray.fromObject(resourceList));
        content.put("pageNum", pageNum);
        result.put("result", content);
        response.getWriter().print(result.toString());
    }

    /**
     * By黄振  本地数据库中，根据节点名称查询节点最近7天历史详情
     * @param request
     * @param response
     * @throws IOException
     */
    private void getHostHistoryState(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String hostName = request.getParameter("hostName");
        //ScheduleClusterHostDao scheduleClusterHostDao = new ScheduleClusterHostDao();
        List<Object> historyResource = scheduleClusterHostDao.getHostHistoryState(hostName);
        List<Object> createTime = new ArrayList<>();
        List<Object> ut = new ArrayList<>();
        List<Object> swp = new ArrayList<>();
        List<Object> mem = new ArrayList<>();
        for(Object oneHistory : historyResource){
            createTime.add(((BaseResourceState) oneHistory).getCreateTime().toString());
            ut.add(((BaseResourceState) oneHistory).getUt());
            swp.add(((BaseResourceState) oneHistory).getSwp());
            mem.add(((BaseResourceState) oneHistory).getMem());
        }
        JSONObject result = new JSONObject();
        JSONObject content = new JSONObject();
        content.put("createTime", createTime);
        content.put("ut", ut);
        content.put("swp", swp);
        content.put("mem", mem);
        result.put("result", content);
        response.getWriter().print(result.toString());
    }
}
